package com.tuanzi.loan.business.service.system.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.retry.RetryException;
import org.springframework.retry.annotation.Retryable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.tuanzi.loan.business.entity.system.Role;
import com.tuanzi.loan.business.repository.system.RoleRepository;
import com.tuanzi.loan.business.service.system.RoleService;
import com.tuanzi.loan.core.aspect.Debug;
import com.tuanzi.loan.core.repository.JPABaseRepository;
import com.tuanzi.loan.core.service.BaseServiceImpl;
import com.tuanzi.loan.core.vo.PageSearcher;
import com.tuanzi.loan.vo.request.system.RoleSearchRequest;
import com.tuanzi.loan.vo.response.system.RoleSearchResponse;

/**
 * @author Ray 2017/09/21
 */
@Debug
@Service
@Transactional
@Retryable(value = { RetryException.class })
public class RoleServiceImpl extends BaseServiceImpl<Role, String> implements RoleService {

	@Autowired
	private RoleRepository roleRepository;

	@Override
	protected JPABaseRepository<Role, String> getRepository() {
		return roleRepository;
	}

	@Override
	public Page<RoleSearchResponse> list(PageSearcher<RoleSearchRequest> pageSearcher) {
		return roleRepository.list(pageSearcher.getSearchCondition(), pageSearcher.getPageable());
	}

	@Override
	public Role findByCode(String code) {
		return roleRepository.findByCode(code);
	}

	@Override
	public List<Role> findAllByUserId(String userId) {
		return roleRepository.findAllByUserId(userId);
	}

	@Override
	public boolean hasRole(String account, String roleCode) {
		return roleRepository.countByAccountAndRoleCode(account, roleCode) > 0;
	}

	@Override
	public List<Role> findAllByCodes(List<String> codes) {
		return roleRepository.findAllByCodeIn(codes);
	}
}
